#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>

char Serial_RxPacket[100]; //"@MSG\r\n"
char Serial2_RxPacket[100]; //"@MSG\r\n"
char Serial3_RxPacket[100]; //"@MSG\r\n"
uint8_t Serial_RxFlag;
uint8_t Serial2_RxFlag;
uint8_t Serial3_RxFlag;

void NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel                   = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_Init(&NVIC_InitStructure);
}

/**
 * @brief	USART1 串口初始化函数
 * @param 	None
 * @return None
 */
void USART_1_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate            = 9600;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_Parity              = USART_Parity_No;
    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
    USART_Init(USART1, &USART_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1, ENABLE);
}

/**
 * @brief	USART2 串口初始化函数
 * @param 	None
 * @return None
 */
void USART_2_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_3;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate            = 9600;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_Parity              = USART_Parity_No;
    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
    USART_Init(USART2, &USART_InitStructure);

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART2, ENABLE);
}

void USART_3_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_11;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate            = 9600;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_Parity              = USART_Parity_No;
    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
    USART_Init(USART3, &USART_InitStructure);

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART3, ENABLE);
}

void Serial_SendByte(USART_TypeDef *Usart, uint8_t Byte)
{
    USART_SendData(Usart, Byte);
    while (USART_GetFlagStatus(Usart, USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(USART_TypeDef *Usart, uint8_t *Array, uint16_t Length)
{
    uint16_t i;
    for (i = 0; i < Length; i++) {
        Serial_SendByte(Usart, Array[i]);
    }
}

void Serial_SendString(USART_TypeDef *Usart, char *String)
{
    uint8_t i;
    for (i = 0; String[i] != '\0'; i++) {
        Serial_SendByte(Usart, String[i]);
    }
}

uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
    uint32_t Result = 1;
    while (Y--) {
        Result *= X;
    }
    return Result;
}

void Serial_SendNumber(USART_TypeDef *Usart, uint32_t Number, uint8_t Length)
{
    uint8_t i;
    for (i = 0; i < Length; i++) {
        Serial_SendByte(Usart, Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
    }
}

int fputc(int ch, FILE *f)
{
    Serial_SendByte(USART1, ch);
    return ch;
}

void Serial_Printf(USART_TypeDef *Usart, char *format, ...)
{
    char String[100];
    va_list arg;
    va_start(arg, format);
    vsprintf(String, format, arg);
    va_end(arg);
    Serial_SendString(Usart, String);
}

void USART1_IRQHandler(void)
{
    static uint8_t RxState   = 0;
    static uint8_t pRxPacket = 0;
    if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET) {
        uint8_t RxData = USART_ReceiveData(USART1);
        if (RxState == 0) {
            if (RxData == 0x42 && Serial_RxFlag == 0) {
                RxState   = 1;
                pRxPacket = 0;
                Serial_RxPacket[pRxPacket++] = RxData;

            }
        } else if (RxState == 1) {
            if (pRxPacket == 31) {
                RxState       = 0;
                Serial_RxFlag = 1;

            } else {
                Serial_RxPacket[pRxPacket] = RxData;
                pRxPacket++;
            }
        }

        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}
void USART3_IRQHandler(void)
{
    static uint8_t RxState3   = 0;
    static uint8_t pRxPacket3 = 0;
    if (USART_GetITStatus(USART3, USART_IT_RXNE) == SET) {
        uint8_t RxData = USART_ReceiveData(USART3);

        if (RxState3 == 0) {
            if (RxData == '$' && Serial3_RxFlag == 0) {
                RxState3   = 1;
                pRxPacket3 = 0;
            }
        } else if (RxState3 == 1) {
            if (pRxPacket3 <= 5) {
                Serial3_RxPacket[pRxPacket3] = RxData;
            } else if (Serial3_RxPacket[3] == 'M' && Serial3_RxPacket[4] == 'C') {
                Serial3_RxPacket[pRxPacket3] = RxData;
            } else {
                RxState3 = 0;
            }
            if (RxData == '\r' || pRxPacket3 > 90) {
                RxState3 = 2;
                pRxPacket3--;
            }
            pRxPacket3++;
        } else if (RxState3 == 2) {
            if (RxData == '\n') {
                RxState3                     = 0;
                Serial3_RxPacket[pRxPacket3] = '\0';
                Serial3_RxFlag              = 1;
            }
        }

        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
    }
}

void USART2_IRQHandler(void)
{
    static uint8_t RxState2   = 0;
    static uint8_t pRxPacket2 = 0;
    if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET) {
        uint8_t RxData = USART_ReceiveData(USART2);

        if (RxState2 == 0) {
            if (RxData == '@' && Serial2_RxFlag == 0) {
                RxState2   = 1;
                pRxPacket2 = 0;
            }
        } else if (RxState2 == 1) {
            if (RxData == '\r') {
                RxState2 = 2;
            } else {
                Serial2_RxPacket[pRxPacket2] = RxData;
                pRxPacket2++;
            }
        } else if (RxState2 == 2) {
            if (RxData == '\n') {
                RxState2                    = 0;
                Serial2_RxPacket[pRxPacket2] = '\0';
                Serial2_RxFlag              = 1;
            }
        }

        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    }
}